package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsShippedBills;
import com.jy.wms.service.WmsShippedBillsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName: WmsShippedBillsController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author YJS
 * @Date Fri Aug 31 12:00:16 GMT+08:00 2018
 */
@RestController
@RequestMapping("/wmsshippedbills")
@Api(value = "/wmsshippedbills", description = "")
public class  WmsShippedBillsController extends BaseController<WmsShippedBillsService,WmsShippedBills> {

    @PostMapping(value = {"list", ""})
    @ApiOperation(value = "查询做账汇总信息",response = JSONObject.class, notes = "查询做账汇总信息")
    public @ResponseBody JSONObject getWmsShippedBills(@RequestBody WmsShippedBills entity,HttpSession session) {
        Page<WmsShippedBills> list = new Page<WmsShippedBills>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            list=baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除信息",response = JSONObject.class, notes = "删除信息")
    public @ResponseBody
    JsonResult deleteWmsShippedBills(@RequestBody List<String> ids) {
        Integer flag=baseService.baseDeleteBathPrimaryKeys(ids);
        return renderSuccess("删除成功");
    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增信息",response = JSONObject.class, notes = "新增信息")
    public @ResponseBody
    JsonResult addWmsShippedBills(@RequestBody WmsShippedBills entity) {
        entity.setId(UUIDUtils.uuid());
        baseService.baseInsert(entity);
        return renderSuccess("添加成功");
    }

    @GetMapping(value = "getWmsShippedBills/{id}")
    @ApiOperation(value = "查看单个信息",response = JSONObject.class, notes = "GET查看单个信息")
    public @ResponseBody WmsShippedBills getWmsShippedBillsById(@PathVariable("id") String id) {
        WmsShippedBills entity=baseService.baseSelectByPrimaryKey(id);
        return entity;
    }
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑信息",response = JSONObject.class, notes = "编辑信息")
    public @ResponseBody
    JsonResult editWmsShippedBills(@RequestBody WmsShippedBills entity) {
        Integer flag=baseService.baseUpdateByPrimaryKeySelective(entity);
        return renderSuccess("修改成功");
    }
    @PostMapping(value = "wmsShippedBillsExport")
    @ApiOperation(value = "导出做账汇总信息", response = JSONObject.class, notes = "导出做账汇总信息")
    public @ResponseBody void wmsShippedBillsExport(@RequestBody WmsShippedBills entity,HttpSession session, HttpServletResponse response) throws IOException {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        entity.setCkid(ckid);
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        entity.setHzid(hzid);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("做账汇总信息", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsShippedBills> list = baseService.wmsShippedBillsExport(entity);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsShippedBills.class, list);
        workbook.write(response.getOutputStream());
    }
}
